import filterData from '@/common/filterData';
import formatter from '@/common/formatter';
import { bizType } from '@/common/getData';
import department from '@/common/getDepartment';
import targetRule from '@/common/targetRule';
import { dayjs } from 'element-plus';
const { department1, department2 } = department;
const allRegion = department1.filter((i) => i !== '全市');
const allGrid = department2.flat();
const {
	excelDate,
	toExcelDate,
	valueToText: v2t,
	valueToWan: v2Wan,
} = formatter;

const L = {
	// LY_kx_xp: 1009, // 2023年2月宽带+新业务派单日均
	// LY_kx_gd: 746, // 2023年2月宽带+新业务归档日均
	// LY_ts_xp: 353, // 2023年2月投诉派单日均
	// LY_ts_gd: 344, // 2023年2月投诉归档日均
	// LY_kdjsl: 0.9882, // 2023年2月宽带及时率
	// LY_tsjsl: 0.9722, // 2023年2月投诉及时率
};

const lastMonthStart = toExcelDate(
	dayjs().startOf('month').subtract(1, 'month').format('YYYY-MM-DD')
);
const lastMonthEnd = toExcelDate(
	dayjs().endOf('month').subtract(1, 'month').format('YYYY-MM-DD')
);

const M = {
	JanBegin: lastMonthStart,
	JanEnd: lastMonthEnd,
	total: lastMonthEnd - lastMonthStart + 1,
};

function huanbiText(v, affix = '') {
	v = /\./.test(v) ? (v + '').replace(/0+$/, '') : v;
	return `环比昨日${v === 0 ? '持平' : `${v > 0 ? '+' : ''}${v}`}${
		v === 0 ? '' : affix
	}`;
}

function huanbi(text, v, isPercent = false, affix = '') {
	let value = isPercent ? (v * 100).toFixed(2) : v + '';
	value = /\./.test(value) ? value.replace(/\.0+$/, '') : value;
	return `${text}${
		v === 0 ? '持平' : `${v > 0 ? '+' : ''}${value}${v === 0 ? '' : affix}`
	}`;
}

function reachText(v, opt) {
	let text = v < opt.baseTarget ? `$未达目标值$` : '已达目标值';
	text = opt.challengeTarget && v >= opt.challengeTarget ? '已达挑战值' : text;
	return text;
}

function getDayCount(dates = [], data) {
	let gridCount = {},
		regionCount = {};
	const matchDateData = data.filter((d) => dates.includes(d.date));
	matchDateData.forEach((i) => {
		if (regionCount[i.region] === undefined) {
			regionCount[i.region] = i.count;
		} else {
			regionCount[i.region] += i.count;
		}

		if (gridCount[i.grid] === undefined) {
			gridCount[i.grid] = i.count;
		} else {
			gridCount[i.grid] += i.count;
		}
	});
	let descRegion = Object.keys(regionCount)
		.map((region) => {
			return { region, count: regionCount[region] };
		})
		.sort((a, b) => b.count - a.count);
	let descGrid = Object.keys(gridCount)
		.map((grid) => {
			return { grid, count: gridCount[grid] };
		})
		.sort((a, b) => b.count - a.count);
	return { descRegion, descGrid };
}

function countZxfz(data) {
	const day7Data = data.length >= 7 ? data : data.slice(data.length - 7);
	const field = {
		iptvzaixianshu: 0,
		jiajikezongliuliang: 0,
		jiakuanzaixianshu: 0,
	};
	const countData = day7Data.reduce((p, c) => {
		p.iptvzaixianshu += c.iptvzaixianshu;
		p.jiakuanzaixianshu += c.jiakuanzaixianshu;
		p.jiajikezongliuliang += c.jiajikezongliuliang;
		return p;
	}, field);
	countData.iptvzaixianshu = v2Wan(countData.iptvzaixianshu);
	countData.jiakuanzaixianshu = v2Wan(countData.jiakuanzaixianshu);
	countData.jiajikezongliuliang = countData.jiajikezongliuliang;
	return countData;
}

function countFxrs(data) {
	const lastDate = Math.max(...new Set(data.map((i) => i.date)));
	const lastData = data.filter((i) => i.date === lastDate);
	const regionData = [],
		rmap = {},
		gridData = [],
		gmap = {};
	lastData.forEach((c) => {
		if (rmap[c.region]) {
			rmap[c.region].renshu += c.renshu;
			rmap[c.region].zhanbi += c.zhanbi;
		} else {
			rmap[c.region] = {
				zhanbi: c.zhanbi,
				renshu: c.renshu,
			};
		}
		if (gmap[c.grid]) {
			gmap[c.region].renshu += c.renshu;
			gmap[c.region].zhanbi += c.zhanbi;
		} else {
			gmap[c.region] = {
				zhanbi: c.zhanbi,
				renshu: c.renshu,
			};
		}
	});
	const descRegionData = Object.keys(rmap)
		.map((region) => {
			return {
				region,
				renshu: rmap[region].renshu,
				zhanbi: rmap[region].zhanbi,
			};
		})
		.sort((a, b) => b.renshu - a.renshu);

	const descGridData = Object.keys(gmap)
		.map((grid) => {
			return { grid, renshu: rmap[grid].renshu, zhanbi: rmap[grid].zhanbi };
		})
		.sort((a, b) => b.renshu - a.renshu);

	const count = data.reduce((p, c) => p + c.fanxiangrenshu2024, 0);
	return {
		lastDate,
		renshu: descRegionData.reduce((p, c) => (p += c.renshu), 0),
		descRegionData,
		descGridData,
	};
}

function countDsrs(data) {
	const desc = [...data].sort(
		(a, b) => a.fanxiangrenshu2024 - b.fanxiangrenshu2024
	);
	const count = data.reduce((p, c) => p + c.fanxiangrenshu2024, 0);
	return {
		count,
		desc,
	};
}

function getConclusion(name, { worst2Region }, last) {
	const p = last ? '。' : '；';
	return worst2Region.length
		? `，其中$&${worst2Region.map((i) => i.name).join('、')}&${name}较差$${p}`
		: p;
}

function getWorst2YuJing(data) {
	const worst2Count = Object.keys(
		data.reduce((p, c) => {
			!p[c.count] && (p[c.count] = true);
			return p;
		}, {})
	)
		.map((i) => Number(i))
		.sort((a, b) => b - a)
		.slice(0, 2);
	return data
		.filter((i) => worst2Count.includes(i.count))
		.map((i) => i.region)
		.join('、');
}

function getPool1({ collection, bizList, type, lastYearData, nlDateMap }) {
	const calcData = bizList.reduce((p, biz) => {
		const countData = filterData
			.countByField('date', collection[biz], false)
			.sort((a, b) => a.date - b.date);
		const day7 =
			countData.length <= 7 ? countData : countData.slice(countData.length - 7);
		const day7Average = day7.length
			? (day7.reduce((p, c) => p + c.count, 0) / day7.length).toFixed(0)
			: 0;
		const day7Count = day7.length ? day7.reduce((p, c) => p + c.count, 0) : 0;
		const lastDay = day7[day7.length - 1] || { count: 0 };
		const lastDay2 = day7[day7.length - 2] || { count: 0 };
		let lastMonthData = countData.filter(
			(i) => i.date >= M.JanBegin && i.date <= M.JanEnd
		);
		const lastMonthCount = lastMonthData.reduce((p, c) => p + c.count, 0);
		const lastMonthAVG = (lastMonthCount / lastMonthData.length).toFixed(0);

		const date7 = day7.map((i) => i.date);

		let lastDateGrid = [],
			lastDateRegion = [],
			day7Region = [],
			day7Grid = [];
		if (date7.length) {
			const { descGrid: _day7Grid, descRegion: _day7Region } = getDayCount(
				date7,
				collection[biz]
			);
			(day7Region = _day7Region), (day7Grid = _day7Grid);
			const { descGrid: dayGrid, descRegion: dayRegion } = getDayCount(
				[lastDay.date],
				collection[biz]
			);
			(lastDateGrid = dayGrid), (lastDateRegion = dayRegion);
		}

		const yesterday = lastDay.count - lastDay2.count;
		const yesterdayText = huanbi('环比昨日', yesterday);

		p[biz] = {
			day7,
			day7Average,
			day7Count,
			lastDateGrid,
			lastDateRegion,
			day7Region,
			day7Grid,
			lastDay,
			lastDay2,
			yesterday,
			yesterdayText,
			lastMonthCount,
			lastMonthAVG,
		};

		if (type && type !== 'xinyewu') {
			// const solarDate = date7.map(d => formatter.solarLunarDate(d));
			// const lastYear7Day = solarDate.map(d => {
			//   const data = lastYearData.find(i => i.date === nlDateMap[d]);
			//   return data;
			// })
			// p[biz].lastYear7Day = lastYear7Day;
			// p[biz].lastY7D_avg = lastYear7Day.reduce((p, c, i) => {
			//   if (i === lastYear7Day.length - 1) {
			//     p.xp = ((p.xp + c.xp) / 7).toFixed(0);
			//     p.zt = ((p.zt + c.zt) / 7).toFixed(0);
			//     p.gd = ((p.gd + c.gd) / 7).toFixed(0);
			//   } else {
			//     p.xp = p.xp + c.xp;
			//     p.zt = p.zt + c.zt;
			//     p.gd = p.gd + c.gd;
			//   }
			//   return p;
			// }, {
			//   xp: 0, gd: 0, zt: 0,
			// })
		}

		return p;
	}, {});

	return calcData;
}

function getWorstArea(
	dateData,
	department,
	regionCount,
	{ isMinBetter, baseTarget }
) {
	return department
		.map((i) => ({ name: i, value: dateData[i] }))
		.sort((a, b) => (isMinBetter ? b.value - a.value : a.value - b.value))
		.filter((i) => (isMinBetter ? i.value > baseTarget : i.value < baseTarget))
		.slice(0, regionCount);
}

function getPool2(collection, targetOpt) {
	const day7 = collection.slice(collection.length - 7);
	const lastDay = day7[day7.length - 1];
	const lastDayText = v2t(lastDay['全市'], targetOpt.isPercent);
	const lastDay2 = day7[day7.length - 2];
	const yesterday = (
		(lastDay['全市'] - lastDay2['全市']) *
		(targetOpt.ztgdb ? 1 : 100)
	).toFixed(2);
	// const yesterdayText = huanbi('环比昨日', Number(yesterday) === 0 ? 0 : yesterday, true, 'pp');
	const yesterdayText = huanbiText(
		Number(yesterday) === 0 ? 0 : yesterday,
		'pp'
	);

	const worst2Region = getWorstArea(lastDay, allRegion, 2, targetOpt);
	const worst3Grid = getWorstArea(lastDay, allGrid, 3, targetOpt);

	return {
		day7,
		lastDay,
		lastDayText,
		lastDay2,
		yesterdayText,
		worst2Region,
		worst3Grid,
	};
}

export default function getReport({ dataPool1, dataPool2 }) {
	const {
		kuandai_xp,
		kuandai_gd,
		kuandai_zt,
		xinyewu_xp,
		xinyewu_gd,
		xinyewu_zt,
		tousu_xp,
		tousu_gd,
		tousu_zt,
	} = dataPool1;
	// const { nl2023kuandai, nl2023tousu } = dataPool2;
	const prevYear = NLObj.prevYear;
	const prevYearTS = dataPool2[`nl${prevYear}tousu`];
	const prevYearKD = dataPool2[`nl${prevYear}kuandai`];
	const lastYearTSCount = filterData.countNlByDate(prevYearTS, [
		'xp',
		'gd',
		'zt',
		'ztgdb',
	]);
	const lastYearKDCount = filterData.countNlByDate(prevYearKD, [
		'xp',
		'gd',
		'zt',
		'ztgdb',
	]);
	const nlDateMap = lastYearTSCount.reduce((p, c) => {
		p[formatter.solarLunarDate(c.date)] = c.date;
		return p;
	}, {});

	const kd_xp = getPool1({
		collection: kuandai_xp,
		bizList: bizType['kuandai'].concat('all'),
		type: 'kuandai',
		lastYearData: lastYearKDCount,
		nlDateMap,
	});
	const kd_zt = getPool1({
		collection: kuandai_zt,
		bizList: bizType['kuandai'].concat('all'),
		type: 'kuandai',
		lastYearData: lastYearKDCount,
		nlDateMap,
	});
	const kd_gd = getPool1({
		collection: kuandai_gd,
		bizList: bizType['kuandai'].concat('all'),
		type: 'kuandai',
		lastYearData: lastYearKDCount,
		nlDateMap,
	});
	const ts_xp = getPool1({
		collection: tousu_xp,
		bizList: bizType['tousu'].concat('all'),
		type: 'tousu',
		lastYearData: lastYearTSCount,
		nlDateMap,
	});
	const ts_zt = getPool1({
		collection: tousu_zt,
		bizList: bizType['tousu'].concat('all'),
		type: 'tousu',
		lastYearData: lastYearTSCount,
		nlDateMap,
	});
	const ts_gd = getPool1({
		collection: tousu_gd,
		bizList: bizType['tousu'].concat('all'),
		type: 'tousu',
		lastYearData: lastYearTSCount,
		nlDateMap,
	});
	const xyw_xp = getPool1({
		collection: xinyewu_xp,
		bizList: bizType['xinyewu'].concat('all'),
	});
	const xyw_zt = getPool1({
		collection: xinyewu_zt,
		bizList: bizType['xinyewu'].concat('all'),
	});
	const xyw_gd = getPool1({
		collection: xinyewu_gd,
		bizList: bizType['xinyewu'].concat('all'),
	});

	// const k_x_avg_xp = ((kd_xp.all.day7Count + xyw_xp.all.day7Count) / 7).toFixed(
	// 	0
	// );
	// const k_x_avg_gd = ((kd_gd.all.day7Count + xyw_gd.all.day7Count) / 7).toFixed(
	// 	0
	// );

	const k_avg_xp = ((kd_xp.all.day7Count) / 7).toFixed(
		0
	);
	const k_avg_gd = ((kd_gd.all.day7Count) / 7).toFixed(
		0
	);

	// const tb7DayKd_xp = ((k_x_avg_xp - L.LY_kx_xp) / L.LY_kx_xp);
	// const tb7DayKd_gd = ((k_x_avg_gd - L.LY_kx_gd) / L.LY_kx_gd);

	// const ts_avarage_xp = (ts_xp.all.day7Average / 7);
	// const ts_avarage_gd = (ts_xp.all.day7Average / 7);

	// const tb7DayTs_xp = ((ts_xp.all.day7Average - ts_xp.all.lastY7D_avg.xp) / ts_xp.all.lastY7D_avg.xp);
	// const tb7DayTs_gd = ((ts_gd.all.day7Average - ts_gd.all.lastY7D_avg.gd) / ts_xp.all.lastY7D_avg.gd);

	// const LM_kx_xp = (
	// 	(kd_xp.all.lastMonthCount + xyw_xp.all.lastMonthCount) /
	// 	M.total
	// ).toFixed(0); // 上个月宽带+新业务派单
	// const LM_kx_gd = (
	// 	(kd_gd.all.lastMonthCount + xyw_gd.all.lastMonthCount) /
	// 	M.total
	// ).toFixed(0); // 上个月宽带+新业务归档
	const LM_k_xp = (
		(kd_xp.all.lastMonthCount ) /
		M.total
	).toFixed(0); // 上个月宽带派单
	const LM_k_gd = (
		(kd_gd.all.lastMonthCount ) /
		M.total
	).toFixed(0); // 上个月宽带归档
	const LM_ts_xp = ts_xp.all.lastMonthAVG; // 上个月投诉派单
	const LM_ts_gd = ts_gd.all.lastMonthAVG; // 上个月投诉归档

	const yujing = {
		ts: v2t(ts_zt.yujing.lastDay.count / ts_zt.all.lastDay.count, true),
		ts_hb: huanbi(
			'环比昨日',
			ts_zt.yujing.lastDay.count / ts_zt.all.lastDay.count -
				ts_zt.yujing.lastDay2.count / ts_zt.all.lastDay2.count,
			true,
			'pp'
		),
		ts_worst: getWorst2YuJing(ts_zt.yujing.lastDateRegion),
		kd: v2t(kd_zt.yujing.lastDay.count / kd_zt.all.lastDay.count, true),
		kd_hb: huanbi(
			'环比昨日',
			kd_zt.yujing.lastDay.count / kd_zt.all.lastDay.count -
				kd_zt.yujing.lastDay2.count / kd_zt.all.lastDay2.count,
			true,
			'pp'
		),
		kd_worst: getWorst2YuJing(kd_zt.yujing.lastDateRegion),
		xyw: v2t(xyw_zt.yujing.lastDay.count / xyw_zt.all.lastDay.count, true),
		xyw_hb: huanbi(
			'环比昨日',
			xyw_zt.yujing.lastDay.count / xyw_zt.all.lastDay.count -
				xyw_zt.yujing.lastDay2.count / xyw_zt.all.lastDay2.count,
			true,
			'pp'
		),
		xyw_worst: getWorst2YuJing(xyw_zt.yujing.lastDateRegion),
	};

	const pool2s = [
		{ name: 'kuandai_canpinglv', nickName: 'kd_cpl' },
		{ name: 'kuandai_huidanlv', nickName: 'kd_hdl' },
		{ name: 'kuandai_jishilv', nickName: 'kd_jsl' },
		{ name: 'xinyewu_huidanlv', nickName: 'xyw_hdl' },
		{ name: 'xinyewu_jishilv', nickName: 'xyw_jsl' },
		{ name: 'xinyewu_zhijiantongguolv', nickName: 'xyw_zjtglv' },
		{ name: 'chongfutousulv', nickName: 'cftsl' },
		{ name: 'tousu_bohuilv', nickName: 'ts_bhl' },
		{ name: 'tousu_canpinglv', nickName: 'ts_cpl' },
		{ name: 'tousu_jishilv', nickName: 'ts_jsl' },
		{ name: 'kuandai_zaituguidangbi', nickName: 'kd_ztgdb', ztgdb: true },
		{ name: 'xinyewu_zaituguidangbi', nickName: 'xyw_ztgdb', ztgdb: true },
		{ name: 'tousu_zaituguidangbi', nickName: 'ts_ztgdb', ztgdb: true },
	];
	const {
		kd_cpl,
		kd_hdl,
		kd_jsl,
		xyw_hdl,
		xyw_jsl,
		xyw_zjtglv,
		cftsl,
		ts_bhl,
		ts_cpl,
		ts_jsl,
		xyw_ztgdb,
		kd_ztgdb,
		ts_ztgdb,
	} = pool2s.reduce((p, c) => {
		p[c.nickName] = getPool2(dataPool2[c.name], {
			...targetRule[c.name],
			ztgdb: c.ztgdb,
		});
		return p;
	}, {});

	const zxfz = countZxfz(dataPool2.zaixianfengzhi);

	// const fxrs = countFxrs(dataPool2.fanxiangrenshu);
	const dsrs = countDsrs(dataPool2.dianshihuoyueshu);

	let text = `<h1>【春节保障日通报】</h1>`;
	// text += `<h2>一、春节回流情况分析</h2>`
	// text += `<p>截止${formatter.excelDate(fxrs.lastDate, 'M月D日')}，茂名家宽及IPTV业务回流预警情况如下：</p>`
	// text += `<p>（一）返乡人员数&${fxrs.renshu}&，前二区域为:&$${fxrs.descRegionData.slice(0, 2).map(i => i.region).join('、')}$&。`
	// text += `<p>（二）电视沉默预计转活跃客户数量数&${dsrs.count}&，前二区域:&$${dsrs.desc.slice(0, 2).map(i => i.region).join('、')}$&。`
	// text += '<br/>';
	text += `<h2>业务效能分析</h2>`;
	text += `<p class="first-line">近一周（&${excelDate(
		kd_xp.all.day7[0].date,
		'M月D日'
	)}&-&${excelDate(kd_xp.all.lastDay.date, 'M月D日')}&)`;
	text += `日均开通派单&${k_avg_xp}单&，&${huanbi(
		`环比<span title="${LM_k_xp}">上月</span>`,
		(k_avg_xp - LM_k_xp) / LM_k_xp,
		true,
		'pp'
	)}&。`; //`${huanbi(`同比<span title="${L.LY_kx_xp}">去年</span>`, tb7DayKd_xp, true, 'pp')}`
	text += `日均开通归档&${k_avg_gd}单&，&${huanbi(
		`环比<span title="${LM_k_gd}">上月</span>`,
		(k_avg_gd - LM_k_gd) / LM_k_gd,
		true,
		'pp'
	)}&。`; //`${huanbi(`同比<span title="${L.LY_kx_gd}">去年</span>`, tb7DayKd_gd, true, 'pp')}。`;
	text += `${formatter.excelDate(kd_xp.all.lastDay.date, 'M月D日')}当天新派${
		kd_xp.all.lastDay.count
	}单，归档${kd_gd.all.lastDay.count}单。`;
	text += `装移机开通及时率${(kd_jsl.lastDay['全市'] * 100).toFixed(2)}%。`; //`${huanbi(`同比<span title="${L.LY_kdjsl}">去年</span>`, kd_jsl.lastDay['全市'] - L.LY_kdjsl, true, 'pp')}。`

	text += '<p>';
	text += `（一）`;
	text += `当天全市宽带新派&${kd_xp.all.lastDay.count}单&，&${kd_xp.all.yesterdayText}&。归档&${kd_gd.all.lastDay.count}单&，&${kd_gd.all.yesterdayText}&。`;
	text += `其中当日`;
	text += `手宽新派&${kd_xp.shoukuan.lastDay.count}单&，归档&${kd_gd.shoukuan.lastDay.count}单&；`;
	text += `全家享新派&${kd_xp.quanjiaxiang.lastDay.count}单&，归档&${kd_gd.quanjiaxiang.lastDay.count}单&；`;
	text += `金牛客户新派&${kd_xp.jinniu.lastDay.count}单&，归档&${kd_gd.jinniu.lastDay.count}单&；`;
	text += `重点区域新派&${kd_xp.kaimenhong.lastDay.count}单&，归档&${kd_gd.kaimenhong.lastDay.count}单&。`;

	text += `宽带开通在途&${kd_zt.all.lastDay.count}单&，&${
		kd_zt.all.yesterdayText
	}&， 其中$${kd_zt.all.day7Region
		.slice(0, 2)
		.map((i) => i.region)
		.join('、')}积压工单较多$；`;
	text += `开通预警工单占比&${yujing.kd}&，&${yujing.kd_hb}&，$&${yujing.kd_worst}&预警工单占比较高$；`;
	text +=
		`装移机开通及时率&${kd_jsl.lastDayText}&，&${kd_jsl.yesterdayText}&` +
		getConclusion('及时率', kd_jsl);
	text +=
		`回单率&${kd_hdl.lastDayText}&，&${kd_hdl.yesterdayText}&` +
		getConclusion('回单率', kd_hdl);
	text +=
		`在途归档比&${kd_ztgdb.lastDayText}&，&${kd_ztgdb.yesterdayText}&` +
		getConclusion('在途归档比消化能力', kd_ztgdb);
	text +=
		`参评率&${kd_cpl.lastDayText}&，&${kd_cpl.yesterdayText}&` +
		getConclusion('参评率', kd_cpl, true);
	text += `</p>`;

	text += '<p>';
	text += `（二）`;
	text += `其中智家业务方面，`;
	text += `当天新派&${xyw_xp.all.lastDay.count}单&，&${xyw_xp.all.yesterdayText}&，`;
	text += `当天归档&${xyw_gd.all.lastDay.count}单&，&${xyw_gd.all.yesterdayText}&，`;
	text += `开通在途&${xyw_zt.all.lastDay.count}单&，&${xyw_zt.all.yesterdayText}&，`;
	text += `其中$${xyw_zt.all.day7Region
		.slice(0, 2)
		.map((i) => i.region)
		.join('、')}积压工单较多$；`;
	text += `开通预警工单占比&${yujing.xyw}&，&${yujing.xyw_hb}&，$&${yujing.xyw_worst}&预警工单占比较高$；`;
	text += `智家及中小企业务开通及时率&${xyw_jsl.lastDayText}&，&${xyw_jsl.yesterdayText}&；`;
	text +=
		`回单率&${xyw_hdl.lastDayText}&，&${xyw_hdl.yesterdayText}&` +
		getConclusion('回单率', xyw_hdl);
	text +=
		`在途归档比&${xyw_ztgdb.lastDayText}&，&${xyw_ztgdb.yesterdayText}&` +
		getConclusion('在途归档比消化能力', xyw_ztgdb);
	text +=
		`质检通过率&${xyw_zjtglv.lastDayText}&，&${xyw_zjtglv.yesterdayText}&` +
		getConclusion('质检通过率', xyw_zjtglv, true);
	text += '</p>';

	text += '<br/>';

	text += `<h2>服务效能分析</h2>`;
	text += '<p class="first-line">';
	text += `近一周（&${excelDate(
		ts_xp.all.day7[0].date,
		'M月D日'
	)}&-&${excelDate(ts_xp.all.lastDay.date, 'M月D日')}&）`;
	text += `日均投诉派单&${ts_xp.all.day7Average}单 &，${huanbi(
		`环比<span title="${LM_ts_xp}">上月</span>`,
		(ts_xp.all.day7Average - LM_ts_xp) / LM_ts_xp,
		true,
		'pp'
	)}。`; //`${huanbi(`同比<span title="${ts_xp.all.lastY7D_avg.xp}">去年</span>`, tb7DayTs_xp, true, 'pp')}。`;
	text += `日均处理归档&${ts_gd.all.day7Average}单 &，${huanbi(
		`环比<span title="${LM_ts_gd}">上月</span>`,
		(ts_gd.all.day7Average - LM_ts_gd) / LM_ts_gd,
		true,
		'pp'
	)}。`; //`${huanbi(`同比<span title="${ts_gd.all.lastY7D_avg.gd}">去年</span>`, tb7DayTs_gd, true, 'pp')}。`;
	text += `${formatter.excelDate(ts_xp.all.lastDay.date, 'M月D日')}当天新派${
		ts_xp.all.lastDay.count
	}单，归档${ts_gd.all.lastDay.count}单，`;
	text += `投诉处理及时率${(ts_jsl.lastDay['全市'] * 100).toFixed(2)}%。`; //`${huanbi(`同比<span title="${L.LY_tsjsl}">去年</span>`, ts_jsl.lastDay['全市'] - L.LY_tsjsl, true, 'pp')}。`
	text += `</p > `;

	text += '<p class="first-line">';
	text += `当天全市投诉在途&${
		ts_zt.all.lastDay.count
	}单 &，其中$&${ts_zt.all.day7Region
		.slice(0, 2)
		.map((i) => i.region)
		.join('、')}& 积压工单较多$；`;
	text += `投诉一级预警工单占比&${yujing.ts}&，&${yujing.ts_hb}&，其中$&${yujing.ts_worst}& 预警工单占比较高$；`;
	text +=
		`在途归档比&${ts_ztgdb.lastDayText}&，&${ts_ztgdb.yesterdayText}& ` +
		getConclusion('在途归档比消化能力', ts_ztgdb);
	const lastDayCC = ts_zt.chaochang.lastDay.count / ts_zt.all.lastDay.count;
	const lastDay2CC = ts_zt.chaochang.lastDay2.count / ts_zt.all.lastDay2.count;
	text += `投诉超长占比&${(lastDayCC * 100).toFixed(2)}%&， `;
	text += `&${huanbi('环比昨日', lastDayCC - lastDay2CC, true, 'pp')}&；`;
	text +=
		`投诉及时率&${ts_jsl.lastDayText}&，&${ts_jsl.yesterdayText}& ` +
		getConclusion('及时率', ts_jsl);
	text +=
		`投诉驳回率&${ts_bhl.lastDayText}&，&${ts_bhl.yesterdayText}& ` +
		getConclusion('驳回率', ts_bhl);
	text +=
		`投诉参评率&${ts_cpl.lastDayText}&，&${ts_cpl.yesterdayText}& ` +
		getConclusion('参评率', ts_cpl);
	text +=
		`投诉重复投诉率&${cftsl.lastDayText}&，&${cftsl.yesterdayText}& ` +
		getConclusion('重复投诉率', cftsl, true);
	text += '</p>';

	text += '<br/>';

	text += `<h2>各县、网格重点关注短板</h2>`;
	text += `<p>（一）开通类</p>`;
	text += `<p class="first-line">`;
	text += `1. 全市宽带回单率为&${(kd_hdl.lastDay['全市'] * 100).toFixed(
		2
	)}&%，${reachText(kd_hdl.lastDay['全市'], targetRule.kuandai_huidanlv)}。`;
	text += `${
		kd_hdl.worst2Region.length
			? `靠后区域：$&${kd_hdl.worst2Region
					.map((i) => i.name + (i.value * 100).toFixed(2) + '%')
					.join('、')}&$；`
			: ''
	} `;
	text += `${
		kd_hdl.worst3Grid.length
			? `靠后网格：$&${kd_hdl.worst3Grid
					.map((i) => i.name + (i.value * 100).toFixed(2) + '%')
					.join('、')}&$。`
			: ''
	} `;
	text += '</p>';

	text += `<p class="first-line">`;
	text += `2. 宽带开通预警工单&${kd_zt.yujing.lastDay.count}单&，&${huanbi(
		'环比昨日',
		kd_zt.yujing.lastDay.count - kd_zt.yujing.lastDay2.count,
		false
	)}&。`;
	text += `靠后区域：$&${kd_zt.yujing.lastDateRegion
		.slice(0, 2)
		.map((i) => i.region + i.count)
		.join('、')}&$；`;
	text += `靠后网格：$&${kd_zt.yujing.lastDateGrid
		.slice(0, 3)
		.map((i) => i.grid + i.count)
		.join('、')}&$。`;
	text += '</p>';

	text += `<p class="first-line">`;
	text += `3. 宽带在途归档比&${kd_ztgdb.lastDayText}&，`;
	text += `${
		kd_ztgdb.worst2Region.length
			? `靠后区域：$&${kd_ztgdb.worst2Region
					.map((i) => i.name + i.value.toFixed(2))
					.join('、')}&$。`
			: ''
	} `;
	text += `${
		kd_ztgdb.worst3Grid.length
			? `靠后网格：$&${kd_ztgdb.worst3Grid
					.map((i) => i.name + i.value.toFixed(2))
					.join('、')}&$。`
			: ''
	} `;
	text += '</p>';

	text += `<p>（二）投诉、满意度类：</p> `;

	text += `<p class="first-line">`;
	text += `1. 全市投诉4小时及时率&${(ts_jsl.lastDay['全市'] * 100).toFixed(
		2
	)}&%，${reachText(ts_jsl.lastDay['全市'], targetRule.tousu_jishilv)}。`;
	text += `${
		ts_jsl.worst2Region.length
			? `靠后区域：$&${ts_jsl.worst2Region
					.map((i) => i.name + (i.value * 100).toFixed(2) + '%')
					.join('、')}&$。`
			: ''
	} `;
	text += `${
		ts_jsl.worst3Grid.length
			? `靠后网格：$&${ts_jsl.worst3Grid
					.map((i) => i.name + (i.value * 100).toFixed(2) + '%')
					.join('、')}&$。`
			: ''
	} `;
	text += '</p>';

	text += `<p class="first-line">`;
	text += `2.投诉预警工单&${ts_zt.yujing.lastDay.count}单&，&${huanbi(
		'环比昨日',
		ts_zt.yujing.lastDay.count - ts_zt.yujing.lastDay2.count,
		false
	)}&。`;
	text += `${
		ts_zt.yujing.lastDateRegion.length
			? `靠后区域：$&${ts_zt.yujing.lastDateRegion
					.slice(0, 2)
					.map((i) => i.region + i.count)
					.join('、')}&$；`
			: ''
	} `;
	text += `${
		ts_zt.yujing.lastDateGrid.length
			? `靠后网格：$&${ts_zt.yujing.lastDateGrid
					.slice(0, 3)
					.map((i) => i.grid + i.count)
					.join('、')}&$。`
			: ''
	} `;
	text += '</p>';

	text += `<p class="first-line"> 3.投诉超长占比&${(lastDayCC * 100).toFixed(
		2
	)}%& ${''}。`;
	text += `${
		ts_zt.chaochang.lastDateRegion.length
			? `靠后区域：$&${ts_zt.chaochang.lastDateRegion
					.slice(0, 2)
					.map((i) => i.region + i.count)
					.join('、')}&$；`
			: ''
	} `;
	text += `${
		ts_zt.chaochang.lastDateGrid.length
			? `靠后网格：$&${ts_zt.chaochang.lastDateGrid
					.slice(0, 3)
					.map((i) => i.grid + i.count)
					.join('、')}&$。`
			: ''
	} `;

	text = text.replaceAll(/&(.+?)&/gi, '$1');
	text = text.replaceAll(/\$(.+?)\$/gi, '<span class="warn">$1</span>');

	return text;
}
// =====================业务效能分析================================

window.getReport = getReport;
